﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AllInOne.HID
{
    /// <summary>
    /// 明华IC卡
    /// </summary>
    public class MingHuaICReader
    {
        public delegate void DataReceived(string data);
        public event DataReceived OnDataReceived;

        public delegate void ConnetState(bool b);
        public event ConnetState OnConnetState;


        //public event RFIDInterface.DataReceived OnDataReceived;
        //public event RFIDInterface.ConnetState OnConnetState;

        private string SerialPortNumber;
        private int icdev;

        public MingHuaICReader(string _SerialPortNumber)
        {
            SerialPortNumber = _SerialPortNumber;
        }
        public string OpenPort()
        {
            var com = SerialPortNumber.ToLower();
            if (short.TryParse(com.Replace("com", ""), out var comIndex))
            {
                icdev = DLLHelper.rf_init((short)(comIndex - 1), 9600);
                if (icdev > 0)
                {
                    DLLHelper.rf_beep(icdev, 5);
                    return "成功";
                }
                else
                {
                    return "失败";
                }
            }
            else
            {
                return "失败";
            }
        }

        public string ReadData()
        {
            if (icdev > 0)
            {
                UInt16 tagtype = 0;
                byte size = 0;
                uint snr = 0;

                mifareone.rf_reset(icdev, 3);
                var st = mifareone.rf_request(icdev, 1, out tagtype);
                if (st != 0)
                {
                    return null;
                }
                st = mifareone.rf_anticoll(icdev, 0, out snr);
                if (st != 0)
                {
                    return null;
                }
                string snrstr = "";
                snrstr = snr.ToString("X");
                //st = mifareone.rf_select(icdev, snr, out size);
                //if (st != 0)
                //{
                //    return null;
                //}
                return snrstr;
            }
            else
            {
                return null;
            }
        }

        public string ClosePort()
        {
            icdev = DLLHelper.rf_exit(icdev);
            return icdev == 0 ? "成功！" : "失败！";
        }

        private volatile bool IsReading = false;
        public void StartRead()
        {
            if (IsReading) return;
            IsReading = true;
            Task.Factory.StartNew(() =>
            {
                while (IsReading)
                {
                    var result = ReadData();
                    if (!string.IsNullOrEmpty(result))
                    {
                        OnDataReceived?.Invoke(result);
                    }
                }
            });
        }

        public void StopRead()
        {
            IsReading = false;
        }
    }
}
